package org.epidroid.epidroid.chronos.ui;

import java.util.ArrayList;

import org.epidroid.epidroid.R;
import org.epidroid.epidroid.chronos.engine.objects.ChronosNode;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ChronosGroupsNavAdapter extends ArrayAdapter<ChronosNode> {
	private ArrayList<ChronosNode> _items = null;
	private ChronosAllGroupsActivity _context = null;

	public ChronosGroupsNavAdapter(ChronosAllGroupsActivity context,
			int textViewResourceId, ArrayList<ChronosNode> items) {
		super(context, textViewResourceId, items);

		_items = items;
		_context = context;
	}

	@Override
	public int getCount() {
		return _items.size();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater inflater = (LayoutInflater) _context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.chronos_group_nav_item, null);
		}

		final ChronosNode t = this._items.get(position);

		TextView content = (TextView) v
				.findViewById(R.id.chronos_group_nav_item_title);
		String title = "";
		for (int i = 0; i < position; i++) {
			title += "    ";
		}
		title += t.getName() + "/";
		content.setText(title);

		content.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (t.getNodes().size() > 0)
					_context.displayNode(t);
			}

		});

		return (v);
	}
}
